package com.drag;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;
import java.util.Timer;

public class DragFrame extends JFrame {
    public JPanel paintPanel;
    private Point p;
    public Point p2;
    Timer timer;
    TimerTask timerTask;
    Thread t;
    volatile boolean repaintPage;
    public DragFrame(){
        super("drag build");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(new Point(500,200));
        setSize(800+16,600+39);
        setPreferredSize(new Dimension(800,600));
        setLayout(null);
        JPanel jback = new JPanel();
        jback.setBackground(Color.PINK);
        jback.setBounds(100,50,100,100);
        paintPanel = new TPanel(null){
            @Override
            public void paint(Graphics g){
                super.paint(g);
                if(p!=null&&p2!=null){
                    int x1 = (int) p.getX();
                    int y1 = (int) p.getY();
                    int x2 = (int) p2.getX();
                    int y2 = (int) p2.getY();
                    int x = (int) Math.abs(DragFrame.this.p2.getX()-DragFrame.this.p.getX());
                    int y = (int) Math.abs(DragFrame.this.p2.getY()-DragFrame.this.p.getY());
                    int px,py;
                    px = Math.min(x2, x1);
                    py = Math.min(y2, y1);
                    g.drawRect(px,py,x,y);
                    g.setColor(new Color(0.5f,0.5f,0.5f,0.5f));
                    g.fillRect(px,py,x,y);
                }
            }
            @Override
            public void justPaint(Graphics g){
                if(p!=null&&p2!=null){
                    int x1 = (int) p.getX();
                    int y1 = (int) p.getY();
                    int x2 = (int) p2.getX();
                    int y2 = (int) p2.getY();
                    int x = (int) Math.abs(DragFrame.this.p2.getX()-DragFrame.this.p.getX());
                    int y = (int) Math.abs(DragFrame.this.p2.getY()-DragFrame.this.p.getY());
                    int px,py;
                    px = Math.min(x2, x1);
                    py = Math.min(y2, y1);
                    g.drawRect(px,py,x,y);
                    g.setColor(new Color(0.5f,0.5f,0.5f,0.5f));
                    g.fillRect(px,py,x,y);
                }
            }
        };
//        paintPanel.add(jback);
        paintPanel.setSize(800,600);
        paintPanel.setPreferredSize(new Dimension(800,600));
        add(paintPanel);
        addListeners();
    }
    public void addListeners(){
//        t = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while(true){
//                    while(repaintPage){
//                        try{
//                            Thread.sleep(10);
//                            DragFrame.this.paintPanel.paint(DragFrame.this.getGraphics());
//                            System.out.println(repaintPage);
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
//                    }
//                    System.out.println(repaintPage);
//                }
//            }
//        });
//        t.start();
        paintPanel.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                repaintPage = true;
//                if(timer==null){
//                    timer = new Timer();
//                    timerTask = new MyTimerTask((TPanel) DragFrame.this.paintPanel);
//                    timer.scheduleAtFixedRate(timerTask,0,200);
//                }
//                System.out.println(e.getPoint());
                DragFrame.this.p2 = e.getPoint();
                DragFrame.this.paintPanel.paint(getGraphics());
//                ((TPanel)(DragFrame.this.paintPanel)).justPaint(DragFrame.this.paintPanel.getGraphics());
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
            }

            @Override
            public int hashCode() {
                return super.hashCode();
            }

            @Override
            public boolean equals(Object obj) {
                return super.equals(obj);
            }

            @Override
            protected Object clone() throws CloneNotSupportedException {
                return super.clone();
            }

            @Override
            public String toString() {
                return super.toString();
            }

            @Override
            protected void finalize() throws Throwable {
                super.finalize();
            }
        });
        paintPanel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
                DragFrame.this.p = e.getPoint();
                System.out.println("Click Listener PRESS"+p);
                repaintPage = false;
//                if(timer!=null){
//                    timerTask.cancel();
//                    timer.cancel();
//                    System.out.println("timer canceled");
//                    timerTask = null;
//                }
//                timer = null;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                DragFrame.this.p = null;
                DragFrame.this.p2 = null;
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
//        paintPanel.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
//            @Override
//            public void ancestorMoved(HierarchyEvent e) {
//                super.ancestorMoved(e);
//            }
//
//            @Override
//            public void ancestorResized(HierarchyEvent e) {
//                super.ancestorResized(e);
//                System.out.println(DragFrame.this.getBounds());
//            }
//
//            @Override
//            public int hashCode() {
//                return super.hashCode();
//            }
//
//            @Override
//            public boolean equals(Object obj) {
//                return super.equals(obj);
//            }
//
//            @Override
//            protected Object clone() throws CloneNotSupportedException {
//                return super.clone();
//            }
//
//            @Override
//            public String toString() {
//                return super.toString();
//            }
//
//            @Override
//            protected void finalize() throws Throwable {
//                super.finalize();
//            }
//        });
//        paintPanel.addHierarchyListener(new HierarchyListener() {
//            @Override
//            public void hierarchyChanged(HierarchyEvent e) {
//                System.out.println("works");
//            }
//        });
//        paintPanel.addAncestorListener(new AncestorListener() {
//            @Override
//            public void ancestorAdded(AncestorEvent event) {
//
//            }
//
//            @Override
//            public void ancestorRemoved(AncestorEvent event) {
//
//            }
//
//            @Override
//            public void ancestorMoved(AncestorEvent event) {
//                System.out.println("move");
////                DragFrame.this.remove(DragFrame.this.paintPanel);
//            }
//        });
    }

    public static void main(String[] args) {
        DragFrame df = new DragFrame();
        df.setVisible(true);
    }
}
class MyTimerTask extends TimerTask {
    private TPanel p1;
    public MyTimerTask(TPanel p1){
        this.p1 = p1;
    }
    @Override
    public void run() {
        work();
    }
    public void work(){
        if(p1!=null){
            p1.paint(p1.getGraphics());
            System.out.println("repaint basic components");
//            p1.justPaint(p1.getGraphics());
        }
    }
}
class TPanel extends JPanel{
    public TPanel(java.awt.LayoutManager layout){
        super(layout);
    }
    public void justPaint(Graphics g){

    }
}
